package band.wukong.practice.rabbitmq.official.torturial.helloworld;

import com.rabbitmq.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author wukong(wukonggg@139.com)
 */
public class HiConsumer {

    private static final Logger logger = LoggerFactory.getLogger(HiConsumer.class);

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HiQConst.HOST_NAME);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(HiQConst.QUEUE_NAME, false, false, false, null);
        logger.info(" [*] Waiting for messages. To exit press CTRL+C");


        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body)
                    throws IOException {
                String message = new String(body, "UTF-8");
                logger.debug(" [x] Received '" + message + "'");
            }
        };
        channel.basicConsume(HiQConst.QUEUE_NAME, true, consumer);
    }
}
